﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CN.DBML;

namespace CityNavigation
{
    public partial class _Default : System.Web.UI.Page
    {
        private CityNavigationDataContext dc = new CityNavigationDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["CityNavigationConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.RadioButtonList1.SelectedIndex = 0;
                this.MultiView1.ActiveViewIndex = 0;
                this.tbxStart.Attributes.Add("onKeyup", string.Format("findNames('{0}',true);", this.tbxStart.ClientID));
                this.tbxEnd.Attributes.Add("onKeyup", string.Format("findNames('{0}',true);", this.tbxEnd.ClientID));
                this.tbxLine.Attributes.Add("onKeyup", string.Format("findNames('{0}',false);", this.tbxLine.ClientID));
                this.tbxStation.Attributes.Add("onKeyup", string.Format("findNames('{0}',true);", this.tbxStation.ClientID));
                this.bindLines();
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.MultiView1.ActiveViewIndex = this.RadioButtonList1.SelectedIndex;
        }

        /// <summary>
        /// 绑定线路
        /// </summary>
        private void bindLines()
        {
            //最新线路
            this.rptNewLines.DataSource = (from item in dc.BusLines
                                           orderby item.AddTime descending
                                           select item).Take(10).ToList();

            this.rptNewLines.DataBind();

            //公交站点
            this.rptHotStation.DataSource = (from item in dc.BusPassStations
                                             group item by item.StationName into bpsList
                                             orderby bpsList.Count() descending
                                             from bps in bpsList
                                             where bps.StationName.Length < 6
                                             select new { bps.StationName }).Distinct().Take(5).ToList();
            this.rptHotStation.DataBind(); 
            
            var linesIndexArr = new string[14] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "B", "K", "Q", "T", "Y" };
            this.rptLinesIndex.DataSource = (from item in linesIndexArr
                                             select new { linesIndex = item }).ToList();
            this.rptLinesIndex.DataBind(); 
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            var lines = from item in dc.BusPassStations
                        join itemTemp1 in dc.BusPassStations on item.LineName equals itemTemp1.LineName into bps
                        from item1 in bps
                        select item1.LineName;

            List<Lines> listLines = new List<Lines>();
            foreach (var item in lines.ToList())
            {
                var line = from bps in dc.BusPassStations
                           where bps.LineName == item
                           orderby bps.SerialNum
                           select bps;
                StringBuilder sb = new StringBuilder();
                foreach (var item1 in line.ToList())
                {
                    sb.Append(item1.StationName);
                    sb.Append("->");
                }
                Lines l= new Lines();
                l.LinesStr = sb.ToString().TrimEnd('>').TrimEnd('-');
                listLines.Add(l);
            }
            this.GridView1.DataSource = listLines;
            this.GridView1.DataBind();
        }

        public class Lines
        {
            public string LinesStr { get; set; }
        }

        protected void btnGo_Click1(object sender, EventArgs e)
        {
            Server.Transfer(string.Format("TransferLines.aspx?ss={0}&es={1}", this.tbxStart.Text.Trim(), this.tbxEnd.Text.Trim()));
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Server.Transfer(string.Format("LineView.aspx?id={0}", this.tbxLine.Text.Trim()));
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            Server.Transfer(string.Format("StationView.aspx?id={0}", this.tbxStation.Text.Trim()));
        }
    }
}
